import api from '../../common/config/api-url'
import Vue from 'vue'

export default {
  list: data => {
    return new Promise(resolve => {
      Vue.prototype.$u.get(api.goods.list, data).then(
        data => {
          if (data) {
            if (data.code === 0) {
              resolve(data.data)
            }
          }
        },
        e => {
          wx.showToast({
            title: e.msg,
            icon: 'none',
            duration: 2000
          })
        }
      )
    })
  },
  detail: data => {
    return new Promise(resolve => {
      Vue.prototype.$u.get(api.goods.detail, data).then(
        r => {
          if (r) {
            if (r.code === 0) {
              resolve(r.data)
            }
          }
        },
        e => {
          wx.showToast({
            title: e.msg,
            icon: 'none',
            duration: 2000
          })
        }
      )
    })
  },
  collectList: data => {
    return new Promise(resolve => {
      Vue.prototype.$u.get(api.goods.collectList, data).then(
        r => {
          if (r) {
            if (r.code === 0) {
              resolve(r.data)
            }
          }
        },
        e => {
          if (e.code === 401) return
          wx.showToast({
            title: e.msg,
            icon: 'none',
            duration: 2000
          })
        }
      )
    })
  },
  lookList: data => {
    return new Promise(resolve => {
      Vue.prototype.$u.get(api.goods.lookList, data).then(
        r => {
          if (r) {
            if (r.code === 0) {
              resolve(r.data)
            }
          }
        },
        e => {
          if (e.code === 401) return
          wx.showToast({
            title: e.msg,
            icon: 'none',
            duration: 2000
          })
        }
      )
    })
  },
  getCode: data => {
    return new Promise(resolve => {
      Vue.prototype.$u.post(api.goods.getCode + '?page=' + data.page + '&scene=' + data.scene).then(
        r => {
          if (r && r.code === 0) {
            resolve(r.data)
          }
        },
        e => {
          wx.showToast({
            title: e.msg,
            icon: 'none',
            duration: 2000
          })
        }
      )
    })
  }
}
